﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SqlCeManager.DbUpdate
{
    public partial class FormDbUpdate : ProgNetComponentsDx.Forms.BaseSqlCeGridForm
    {
        public FormDbUpdate()
        {
            InitializeComponent();
            colDB_UPD_SKRYPT.ColumnEdit = new RepositoryItemByteArrayAsStringEdit();
            (colDB_UPD_SKRYPT.ColumnEdit as RepositoryItemByteArrayAsStringEdit).ShowIcon = false;
        }
        public FormDbUpdate(int polaczenie)
            : this()
        {
            selectDataProvider1.GetSettings(this).Parameters["ID"].Value = polaczenie;
            this.Text += " " + browserProvider1.GetBrowser(Init.InitFormBrowserNames.Glowna.Polaczenia).GetDisplayForValue(polaczenie);
        }

        private void bbiKopiuj_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CopySelectedScripts();
        }
        private void CopySelectedScripts(bool showInfo = true)
        {
            DataRow[] rows = gvMain.GetSelectedDataRows();
            string str = "";
            if (rows != null && rows.Length > 0)
            {
                foreach (DataRow r in rows)
                {
                    if (r["DB_UPD_SKRYPT"] != DBNull.Value)
                    {
                        str = str.AppendLine(((byte[])r["DB_UPD_SKRYPT"]).ByteArrayToString());
                        str = str.AppendLine("GO;");
                    }
                }
                if (str.IsNotNull())
                {
                    Clipboard.SetText(str);
                    if (showInfo)
                        this.ShowDxInfoDialog("Skrypty skopiowane");
                    //Clipboard.SetText(str);
                }
                else
                {
                    this.ShowDxInfoDialog("Brak skryptow");
                }
            }
            else
            {
                this.ShowDxInfoDialog("Wybierz wiersze");
            }
        }
        private void gvMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.C && e.Control)
            {
                CopySelectedScripts(false);
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }
        
    }
}
